﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using FeedsLibrary.Feeds;

namespace FeedsLibrary
{
    public class UserSettings
    {
        private List<FeedDetail> _feedList = new List<FeedDetail>();

        public UserSettings( string dataPath )
        {
            XmlNode nodeFeeds;
            XmlNode nodeFeed;
            XmlNode nodeChan;


            // Create a new XmlTextReader from the specified URL (RSS feed)
            XmlDocument rssDoc = new XmlDocument();

            // Load the XML content into a XmlDocument
            string feedDocPath = Path.Combine( dataPath, @"Feeds.xml" );

            using (XmlTextReader t = new XmlTextReader(feedDocPath))
            {
                rssDoc.Load(t);
            }

            // Loop for the <rss> tag
            for ( int i = 0; i < rssDoc.ChildNodes.Count; i++ )
            {
                // If it is the <Feeds> tag
                if ( rssDoc.ChildNodes[ i ].Name == "Feeds" )
                {
                    // <Feeds> tag found
                    nodeFeeds = rssDoc.ChildNodes[ i ];

                    // Loop for the <Feed> tag
                    for ( int ii = 0; ii < nodeFeeds.ChildNodes.Count; ii++ )
                    {
                        // If it is the channel tag
                        if ( nodeFeeds.ChildNodes[ ii ].Name == "Feed" )
                        {
                            // <Feed> tag found
                            nodeFeed = nodeFeeds.ChildNodes[ ii ];

                            FeedDetail f = new FeedDetail();
                            f.ChannelDetails = new List<ChannelDetail>();

                            f.Name = nodeFeed.Attributes["name"].Value;
                            f.TagsOnly = Boolean.Parse( nodeFeed.Attributes["tagsOnly"].Value);
                            f.Days = Int32.Parse(nodeFeed.Attributes["days"].Value);

                            // Loop for the <Feed> tag
                            for ( int c = 0; c < nodeFeed.ChildNodes.Count; c++ )
                            {
                                // If it is the channel tag
                                if ( nodeFeed.ChildNodes[ c ].Name == "Channel" )
                                {
                                    ChannelDetail cd = new ChannelDetail();
                                    nodeChan = nodeFeed.ChildNodes[ c ];
                                    try
                                    {
                                        // <channel> tag found
                                        cd.Url =new Uri( nodeChan.Attributes[ "URL" ].Value);
                                        if ( nodeChan.Attributes.GetNamedItem( "icon" ) != null )
                                        {
                                            cd.IconUrl = new Uri( nodeChan.Attributes[ "icon" ].Value);
                                        }
                                        f.ChannelDetails.Add( cd );

                                    }
                                    catch (Exception e)
                                    {
                                        
                                        throw new Exception("Error loading the data for " + f.Name,e);
                                    }

                                }
                            }
                            _feedList.Add( f );
                        }
                    }
                }
            }
        }

        public int FeedCount()
        {
            return _feedList.Count;
        }

        public FeedDetail FeedDetails( int index )
        {
            FeedDetail f = _feedList[ index ];
            return f;
        }
    }
}